package SalesBooth;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FruitStandTest {
	public static void main(String[] args) {
		FruitStandTest fst = new FruitStandTest();
		fst.add(); // 添加三种水果
		fst.start();
	}

	
	Scanner sc = new Scanner(System.in);
	FruitDao fdao =new FruitDao();
	PurchaseOrderDao pdao =new PurchaseOrderDao();
	RecordingDao rdao =new RecordingDao();
	
	/**
	 * 初始界面
	 */
	void start() {
		while (true) {
			System.out.println("------------------------");
			System.out.println("请选择功能:");
			System.out.println("1、进货");
			System.out.println("2、水果销售");
			System.out.println("3、显示当前各种水果的库存数");
			System.out.println("4、查询全部销售记录信息");
			System.out.println("5、查询指定日期间隔的纯利润");
			System.out.println("6、按销售记录排序显示前三名");
			System.out.println("7、显示当前各种水果的库存数，成本单价，单价，累计销售数量/金额累计进货数量/金额");
			System.out.println("0、结束当前程序");
			System.out.println("------------------------");
			String str = sc.nextLine();
			switch (str) {
			case "1":
				purChase();
				break;
			case "2":
				sell();
				break;
			case "3":
				disQuan();
				break;
			case "4":
				salesRec();
				break;
			case "5":
				profitQuery();
				break;
			case "6":
				sort();
				break;
			case "7":
				traverSe();
				break;
			case "0":
				System.out.println("程序已结束");
				return;
			default:
				System.out.println("输入错误，请重新尝试");
			}
		}
	}

	/**
	 * 初始三种水果
	 */
	void add() {
		FruitModel fruit1 = new FruitModel();
		fruit1.setName("苹果"); // 水果名
		fruit1.setInve(30.00); // 库存数
		fruit1.setUnitPri(2.50); // 单价
		fdao.insert(fruit1);

		FruitModel fruit2 = new FruitModel();
		fruit2.setName("梨"); // 水果名
		fruit2.setInve(40.00); // 库存数
		fruit2.setUnitPri(1.70); // 单价
		fdao.insert(fruit2);

		FruitModel fruit3 = new FruitModel();
		fruit3.setName("香蕉"); // 水果名
		fruit3.setInve(20.00); // 库存数
		fruit3.setUnitPri(3.30); // 单价
		fdao.insert(fruit3);

	}

	/**
	 * 显示水果的库存量
	 */
	void disQuan() {
		FruitModel model = new FruitModel();
		List<FruitModel> fruitList = fdao.selectList(model);
		for (FruitModel fruit : fruitList) {
			System.out.println("水果名：" + fruit.getName());
			System.out.println("库存量：" + fruit.getInve() + "斤");
		}
	}

	/**
	 * 查重,如果存在则修改
	 */
	int pur(String name,double n) {
		FruitModel model = new FruitModel();
		List<FruitModel> fruitList = fdao.selectList(model);
		for (FruitModel fruit : fruitList) {
			if (fruit.getName().equals(name)) {
				fruit.setInve(n + fruit.getInve());
				fdao.update(fruit);
				return 1;
			}
		}
		return 0;
	}

	/**
	 * 进货单
	 */
	void purChase() {
		while (true) {
			double a = 0;
			System.out.println("请输入水果名称：");
			String name = sc.nextLine();
			System.out.println("请输入进货数：");
			double n = Double.parseDouble(sc.nextLine());
			if(pur(name,n)==0) {
				System.out.println("请输入进货成本单价：");
				a = Double.parseDouble(sc.nextLine());
				System.out.println("请输入销售单价：");
				double b = Double.parseDouble(sc.nextLine());
				
				FruitModel fruit = new FruitModel();
				fruit.setName(name);
				fruit.setUnitPri(b);
				fruit.setInve(n);
				fdao.insert(fruit);
			}

			Date d = new Date();
			Timestamp date = new Timestamp(d.getTime());
			PurchaseOrderModel order = new PurchaseOrderModel();
			order.setDate(date);// 日期
			order.setName(name); // 水果名
			order.setInve(n); // 数量
			order.setCostPri(a); // 成本单价		
			pdao.insert(order);
			System.out.println("进货成功！是否继续添加(y/n)");
			String f = sc.nextLine();
			if (f.equalsIgnoreCase("n")) {
				break;
			}
		}
	}

	/**
	 * 出售遍历，方便选择
	 */
	void sellTra() {
		FruitModel model = new FruitModel();
		List<FruitModel> fruitList = fdao.selectList(model);
		for (FruitModel fruit : fruitList) {
			System.out.println("水果名：" + fruit.getName());
			System.out.println("库存量：" + fruit.getInve() + "斤");
			System.out.println("单价:" + fruit.getUnitPri() + "元/斤");
		}
	}

	/**
	 * 销售记录
	 */
	void reCor(String name, double n, double s) {
		double amount1 = timeJudg(n * s);
		double amount2 = gender(amount1);
		RecordingModel rec = new RecordingModel();
		Date d = new Date();
		Timestamp date = new Timestamp(d.getTime());
		rec.setDate(date);
		rec.setName(name);
		rec.setInve(n);
		rec.setAmount(amount2);
		rdao.insert(rec); // 添加销售记录
	}

	/**
	 * 判断时间是否到17：00
	 */
	double timeJudg(double n) {

		Calendar cal = Calendar.getInstance();
		SimpleDateFormat fmt = new SimpleDateFormat("HHmmss");
		String dateString = fmt.format(cal.getTime());
		long nowTime = Long.parseLong(dateString);
		if (nowTime > 170000) {
			System.out.println("当前时间超过17：00，已为您减价50%");
			n = n * 0.5;
		}
		return n;
	}

	/**
	 * 判断性别，女士优惠20%
	 * 
	 * @return
	 */
	double gender(double n) { // 女士返回true
		System.out.println("请选择性别：(男/女)");
		String gender = sc.nextLine();
		if (gender.equals("女")) {
			System.out.println("已为您减价20%");
			n = n * 0.8;
		}
		return n;
	}

	/**
	 * 出售水果
	 */
	void sell() {
		System.out.println("请根据以下列表自行选择");
		sellTra();
		while (true) {
			System.out.println("请输入想买的水果名：");
			String name = sc.nextLine();
			System.out.println("请输入想买多少斤：");
			double n = Double.parseDouble(sc.nextLine());
			
			FruitModel model = new FruitModel();
			List<FruitModel> fruitList = fdao.selectList(model);
			for (FruitModel fruit : fruitList) {
				if (fruit.getName().equals(name)) {
					fruit.setInve(fruit.getInve() - n);
					fdao.update(fruit);
					reCor(name, n, fruit.getUnitPri()); // 添加销售记录
					break;
				}
			}
			System.out.println("购买成功！是否继续购买(y/n)");
			String f = sc.nextLine();
			if (f.equalsIgnoreCase("n")) {
				break;
			}
		}
	}

	/**
	 * 全部销售记录
	 */
	void salesRec() {
		RecordingModel model = new RecordingModel();
		List<RecordingModel> recordingList = rdao.selectList(model);
		for (RecordingModel rec : recordingList) {
			System.out.println("日期：" + rec.getDate());
			System.out.println("水果名：" + rec.getName());
			System.out.println("销售量：" + rec.getInve());
			System.out.println("销售金额：" + rec.getAmount());
		}
	}

	/**
	 * 显示当前各种水果的库存数，成本单价，单价，累计销售数量/金额 累计进货数量/金额
	 */
	void traverSe() {
		FruitModel fmodel = new FruitModel();
		List<FruitModel> fruitList = fdao.selectList(fmodel);
		
		PurchaseOrderModel pmodel = new PurchaseOrderModel();
		List<PurchaseOrderModel> orderList = pdao.selectList(pmodel);
		
		for (FruitModel fruit : fruitList) {
			String name = fruit.getName();
			double a = 0; // 成本单价
			double n = 0; // 累计进货数量
			double salesVolume = 0;	//累计销售数量
			for (PurchaseOrderModel order : orderList) {
				if (order.getName().equals(name)) {
					a = order.getCostPri();
					n = n + order.getInve();
				}
			}
			
			RecordingModel rmodel = new RecordingModel();
			List<RecordingModel> recordingList = rdao.selectList(rmodel);
			double amount = 0; // 累计销售金额
			for (RecordingModel rec : recordingList) {
				if (rec.getName().equals(name)) {
					amount = amount + rec.getAmount();
					salesVolume = salesVolume + rec.getInve();
				}
			}
			double d = a * n; // 累计进货金额
			System.out.println("水果名：" + fruit.getName());
			System.out.println("库存数：" + fruit.getInve());
			System.out.println("成本单价：" + a);
			System.out.println("单价：" + fruit.getUnitPri());
			System.out.println("累计销售数量：" + salesVolume);
			System.out.println("累计销售金额：" + amount);
			System.out.println("累计进货数量：" + n);
			System.out.println("累计进货金额：" + d);
		}
	}
	
	
	/**
	 * 获得某种水果的累计销售数量
	 */
	double salesVolume(String name) {
		double sum = 0;
		RecordingModel model = new RecordingModel();
		List<RecordingModel> recordingList = rdao.selectList(model);
		for(RecordingModel rec:recordingList) {
			if(name.equals(rec.getName()) ) {
				sum=sum+rec.getInve();
			}
		}
		return sum;
	}
	
	
	/**
	 * 按销售记录排序
	 */
	void sort() {
		ArrayList<FruitModel> fruitList2 = new ArrayList<>();
		FruitModel fmodel = new FruitModel();
		List<FruitModel> fruitList = fdao.selectList(fmodel);
		for(FruitModel fruit:fruitList) {
			FruitModel f = new FruitModel();
			String name =fruit.getName();
			f.setInve(salesVolume(name));
			f.setName(name);
			fruitList2.add(f);
		}
		
		int n = fruitList2.size();
		for (int i = 0; i < n - 1; i++) {
			for (int j = 0; j < n - 1 - i; j++) {
				FruitModel fruit1 = fruitList2.get(j);
				FruitModel fruit2 = (FruitModel) fruitList.get(j + 1);
				if (fruit1.getInve() < fruit2.getInve()) {
					FruitModel tmp = fruit1;
					fruitList.set(j, fruitList.get(j + 1));
					fruitList.set(j + 1, tmp);
				}
			}
		}
		dis3(fruitList2);
	}

	/**
	 * 显示前三
	 */
	void dis3(ArrayList<FruitModel> fruitList) {
		for (int i = 0; i < 3; i++) {
			System.out.println("水果名：" + fruitList.get(i).getName());
			System.out.println("累计销售数量：" + fruitList.get(i).getInve() + "斤");
		}
	}

	/**
	 * 将字符串中特殊符号去掉，并转换成long
	 * 
	 * @param s
	 * @return
	 */
	long conversion(String s) { // 正则表达式
		Pattern pattern = Pattern.compile("[^0-9]");
		Matcher matcher = pattern.matcher(s);
		String str = matcher.replaceAll("");
		long re = Long.parseLong(str);
		return re;
	}

	/**
	 * 查询某个时间段的纯利润
	 */
	void profitQuery() {
		double cost = 0;
		double profit = 0;
		System.out.println("请输入想要查看的纯利润的时间段(yyyy-MM-dd HH:mm)");
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		System.out.println("请输入时间起点：");
		String before = sc.nextLine();
		long a = conversion(before);

		System.out.println("请输入时间终点：");
		String after = sc.nextLine();
		long b = conversion(after);
		
		PurchaseOrderModel pmodel = new PurchaseOrderModel();
		List<PurchaseOrderModel> orderList = pdao.selectList(pmodel);
		for (PurchaseOrderModel order : orderList) {
			Date date = order.getDate();
			String strdate = fmt.format(date);
			long c = conversion(strdate);
			if (c >= a) {
				if (c <= b) {
					cost = cost + order.getCostPri() * order.getInve(); // 得到成本
				}
			}
		}
		RecordingModel model = new RecordingModel();
		List<RecordingModel> recordingList = rdao.selectList(model);
		for (RecordingModel rec : recordingList) {
			Date date = rec.getDate();
			String strdate = fmt.format(date);
			long c = conversion(strdate);
			if (c >= a) {
				if (c <= b) {
					profit = profit + rec.getAmount(); // 得到利润
				}
			}
		}
		double netProfit = profit - cost;
		BigDecimal fare = new BigDecimal(netProfit).setScale(2, BigDecimal.ROUND_HALF_UP);
		System.out.println("从" + before + "到" + after + "的纯利润为" + fare);
	}

}